import React,{ Component } from 'react';

class Clock extends Component{
    constructor(){
        super();
        this.state = {
            date: new Date()
        }
    }

    componentWillMount(){
        this.timerId = setInterval(
            () => {
                this.setState({date:new Date()})
            },1000
        );
    }

    componentDidMount(){}

    componentWillUnmount(){
        clearInterval(this.timerId);
    }

    render(){
        return (
            <div>
                <h1>
                    <p>现在的时间是：</p>
                    <span>{this.state.date.toLocaleTimeString()}</span>
                </h1>
            </div>
        );
    }
}

export default Clock;